<template>
 <div class="article">
    <!-- 按钮 -->
    <div class="btns">
      <el-button size="small" type="primary" @click="toAdd">新增</el-button>
    </div>
    <!-- /按钮 -->

    <!-- 表格 -->
    <el-table size="small" :data="articles.list">
      <el-table-column label="序号" type="index" :index="1"></el-table-column>
      <el-table-column label="新闻标题" prop="title" ></el-table-column>
      <el-table-column label="发布时间" prop="publishTime" width="120"></el-table-column>
      <el-table-column label="阅读次数" prop="readTimes" width="80"></el-table-column>
      <el-table-column label="状态" prop="status" width="100"></el-table-column>
      <el-table-column label="点赞次数" prop="thumpUp" width="100"></el-table-column>
      <el-table-column label="操作" width="100" align="center" fixed="right">
        <template v-slot:default="scope">
          <el-button @click="toEdit(scope.row)" size="mini" type="text" >修改</el-button>
          <el-button @click="toDel(scope.row)" size="mini" type="text">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- /表格 -->
    
    <el-pagination
      v-if="articles.total > param.pageSize"
      layout="prev, pager, next"
      @current-change="currentChangeHandler"
      :current-page="param.page"
      :page-size="param.pageSize"
      :total="articles.total">
    </el-pagination>
   

    <!-- 模态框 -->
    <el-dialog title="发布新闻资讯" :visible.sync="visible" width="80%">
      {{form}}
      <el-form label-width="80px" size="small">
        <el-form-item label="标题">
          <el-input v-model="form.title"></el-input>
        </el-form-item>
        <el-form-item label="所属分类">
          <el-select v-model="form.categoryId">
            <el-option v-for="c in categorys" :key="c.id" :label="c.name" :value="c.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="资讯">
          <!-- 富文本编辑器 -->
           <tinymce v-model="form.content" :height="240" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="visible = false" size="small">取 消</el-button>
        <el-button type="primary" @click="submit" size="small">确 定</el-button>
      </span>
    </el-dialog>
    <!-- /模态框 -->
  </div>
  </template>

<script >
//导入ajax工具（本质ajax -> axios -> reqeust）
import {get,post} from '@/utils/request'
import Tinymce from '@/components/Tinymce'
export default {
  components:{Tinymce},
  data(){
    return {
      visible:false,
      param:{
        page:1,
        pageSize:10
      },
      articles:{},
      categorys:[],
      form:{},
      title:"录入新闻资讯"
    }
  },
  created(){
    // 在vue实例创建完毕后加载分类信息
    this.loadArticleData();
    this.loadCategoryData();
  },
  methods:{
    // 提交表单
    submit(){
      let url = "/article/saveOrUpdate";
      // 修改form，不用直接操作form，先进行克隆（深拷贝）
      let data = Object.assign({},this.form);
      // 提交
      post(url,data).then(response => {
        this.$message({type:"success",message:response.message});
        this.loadArticleData();
        this.visible = false;
      })
    },
    // 去添加
    toAdd(){
      this.title = "录入新闻资讯";
      this.form = {};
      this.visible = true;
    },
    // 打开模态框准备修改
    toEdit(row){
      this.title = "修改新闻资讯";
      // 将row克隆后赋值给form，避免直接修改row影响页面变化
      this.form = Object.assign({},row);
      this.visible = true;
    },
    toDel(row){
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let url = "/article/deleteById";
        get(url,{id:row.id}).then(response => {
          this.$message({type:"success",message:response.message});
          this.loadArticleData();
        })
      })
    },
    // 加载新闻资讯
    loadArticleData(){
      let url = "/article/pageQuery";
      get(url,this.param).then(response => {
        this.articles = response.data;
      });
    },
    // 加载资讯分类
    loadCategoryData(){
      let url = "/category/findAll";
      get(url).then(response => {
        this.categorys = response.data;
      });
    },
    // 监听翻页
    currentChangeHandler(page){
      this.param.page = page;
      this.loadArticleData();
    }
  }
}
</script>